<?php

namespace app\index\controller;

use app\Api;

class Common extends Api
{
    protected $app_id; // app_id

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 验证passport 返回登录的用户ID
     * @return mixed
     */
    protected function _checkPassport($passport)
    {
        if (!preg_match('/^[0-9a-zA-Z]{32}$/', $passport)) {
            $this->_returnError('10010', 'passport不合法');
        }
        $user_id = \think\Db::name('user_info')->where(['passport' => $passport])->value('user_id');
        if (isset($user_id) && preg_match('/^[1-9]\d*$/', $user_id)) {
            return $user_id;
        }
        $this->_returnError(10010, 'passport不合法');
    }
    /**
     * 获取随机卡密
     * @access protected
     * @param int $n 随机数的长度(默认8位)
     * @since 1.0
     * @return string
     */
    protected function _getRandomSecretKey($n = 8)
    {
        $str = '123456789ABCDEFGHIJKLMNPQRSTUVWXYZ';//输出字符集 AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz
        $len = strlen($str) - 1;
        $s = '';
        for ($i = 0; $i < $n; $i++) {
            $s .= $str[rand(0, $len)];
        }
        return $s;
    }

    /**
     * 获取参数
     * @param string $key 键
     * @param string $default 默认值
     * @return string
     */
    protected function _getParams($key, $default = '')
    {
        if (isset($this->_parameters[$key])) {
            return $this->_parameters[$key];
        } else {
            return $default;
        }
    }
}
